package org.teachertools.web.actions;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.teachertools.common.ValidationUtils;
import org.teachertools.data.Categorie;
import org.teachertools.data.Competence;
import org.teachertools.data.Eleve;
import org.teachertools.data.Livret;
import org.teachertools.data.Matiere;
import org.teachertools.services.CategorieService;
import org.teachertools.services.CompetenceService;
import org.teachertools.services.EleveService;
import org.teachertools.services.LivretService;
import org.teachertools.services.MatiereService;
import org.teachertools.services.exception.ServiceException;
import org.teachertools.web.ServiceLocatorException;
import org.teachertools.web.struts.BaseAction;

/**
 * @author eveno
 * 
 */
public abstract class CommonAction extends BaseAction {

    protected void addCategoryList(HttpServletRequest request) throws ServiceLocatorException, ServiceException {
        CategorieService categorieService = getServiceLocator().getCategorieService();
        List<Categorie> liste = categorieService.getList();
        request.setAttribute("CATEGORIE", liste);
    }

    protected void addMatiereList(HttpServletRequest request) throws ServiceLocatorException, ServiceException {
    	Long idCategorie = null;
        String idCategorieString = request.getParameter("idCategorie");
        if (ValidationUtils.isNotEmpty(idCategorieString)) {
        	idCategorie = Long.valueOf(idCategorieString);
        } else {
            idCategorie = (Long) request.getAttribute("idCategorie");
        }
    	if (ValidationUtils.isNotEmpty(idCategorie)) {
            MatiereService matiereService = getServiceLocator().getMatiereService();
            List<Matiere> liste = matiereService.getMatiereForCategorieId(idCategorie);
            request.setAttribute("MATIERE", liste);
        }
    }

    protected void addClassList(HttpServletRequest request) throws ServiceLocatorException, ServiceException {
        EleveService eleveService = getServiceLocator().getEleveService();
        List<String> liste = eleveService.getDistinctClassName();
        request.setAttribute("CLASSE", liste);
    }

    protected void addEleveList(HttpServletRequest request) throws ServiceLocatorException, ServiceException {
        String idClasse = request.getParameter("idClasse");
        if (idClasse == null) {
            idClasse = (String) request.getAttribute("idClasse");
        }
        EleveService eleveService = getServiceLocator().getEleveService();
        List<Eleve> liste = null;
        if (StringUtils.isNotEmpty(idClasse)) {
            liste = eleveService.getEleveForClasse(idClasse);
        } else {
            liste = eleveService.getList();
        }
        request.setAttribute("ELEVE", liste);
    }

    protected void addCompetenceList(HttpServletRequest request) throws ServiceLocatorException, ServiceException {
    	Long idMatiere = null;
        String idMatiereString = request.getParameter("idMatiere");
        if (ValidationUtils.isNotEmpty(idMatiereString)) {
        	idMatiere = Long.valueOf(idMatiereString);
        } else {
            idMatiere = (Long) request.getAttribute("idMatiere");
        }
        if (ValidationUtils.isNotEmpty(idMatiere)) {
            CompetenceService competenceService = getServiceLocator().getCompetenceService();
            List<Competence> liste = competenceService.getCompetenceForMatiereId(idMatiere);
            request.setAttribute("COMPETENCE", liste);
        }
    }

    protected void addLivretList(HttpServletRequest request) throws ServiceLocatorException, ServiceException {
        LivretService livretService = getServiceLocator().getLivretService();
        List<Livret> liste = livretService.getList();
        request.setAttribute("LIVRET", liste);
    }

}
